package com.roads.system.service.impl;

import com.roads.common.constant.Constants;
import com.roads.common.constant.UserConstants;
import com.roads.common.utils.DateUtils;
import com.roads.common.utils.StringUtils;
import com.roads.system.domain.SysProfileBoq;
import com.roads.system.mapper.SysProfileBoqMapper;
import com.roads.system.service.ISysProfileBoqService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统形象清单Service业务层处理
 * 
 * @author nhp
 * @date 2024-01-24
 */
@Service
public class SysProfileBoqServiceImpl implements ISysProfileBoqService {

    @Autowired
    private SysProfileBoqMapper mapper;

    /**
     * 根据主键ID查询详情
     * 
     * @param id 系统形象清单主键
     * @return 系统形象清单
     */
    @Override
    public SysProfileBoq getModelById(Integer id) {
        return mapper.getModelById(id);
    }

    /**
     * 查询列表
     * 
     * @param model 系统形象清单
     * @return 系统形象清单
     */
    @Override
    public List<SysProfileBoq> selectModelList(SysProfileBoq model) {
        return mapper.selectModelList(model);
    }

    /**
     * 查询清单树形列表(名称=清单编号+清单名称)
     *
     * @param boq 清单
     * @return 清单集合
     */
    @Override
    public List<SysProfileBoq> selectTreeList(SysProfileBoq boq) {
        return mapper.selectTreeList(boq);
    }

    /**
     * 根据清单编号查询子节点
     *
     * @param boqCode 清单编号
     * @return 清单集合
     */
    @Override
    public List<SysProfileBoq> selectChildrenList(String boqCode) {
        return mapper.selectChildrenList(boqCode);
    }

    /**
     * 校验清单编号是否唯一
     *
     * @param boq 清单实体
     * @return 结果
     */
    @Override
    public String checkBoqCodeUnique(SysProfileBoq boq) {
        Integer id = StringUtils.isNull(boq.getId()) ? -1 : boq.getId();
        SysProfileBoq search = mapper.getModelByCode(boq.getBoqCode());
        if (StringUtils.isNotNull(search) && !search.getId().equals(id)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增
     * 
     * @param model 系统形象清单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(SysProfileBoq model) {
        model.setCreateTime(DateUtils.getNowDate());
        // 父项编号
        String parentCode = model.getParentCode();
        // 如果是根节点
        if (StringUtils.isEmpty(parentCode)) {
            model.setLevel(1);
            model.setAncestors(model.getBoqCode());
        } else {
            // 查询父清单
            SysProfileBoq parentBoq = mapper.getModelByCode(parentCode);
            // 是否更新父清单
            if (Constants.YES.equals(parentBoq.getLeafNode())) {
                parentBoq.setLeafNode(Constants.NO);
                mapper.update(parentBoq);
            }
            model.setLevel(parentBoq.getLevel() + 1);
            model.setAncestors(parentBoq.getAncestors() + "," + model.getBoqCode());
        }
        model.setLeafNode(Constants.YES);
        mapper.insert(model);
        return 1;
    }

    /**
     * 修改
     * 
     * @param model 系统形象清单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(SysProfileBoq model) {
        // 取出原有清单
        SysProfileBoq oldBoq = mapper.getModelById(model.getId());
        int oldLevel = oldBoq.getLevel();
        // 新父项清单处理
        int oldParentLevel = 0;
        String nowAncestors = "";
        String oldAncestors = oldBoq.getAncestors();
        String oldParentCode = StringUtils.isEmpty(oldBoq.getParentCode()) ? "" : oldBoq.getParentCode();
        String nowParentCode = StringUtils.isEmpty(model.getParentCode()) ? "" : model.getParentCode();
        // 如果父项发生变化
        if (!oldParentCode.equals(nowParentCode)) {
            // 原父项清单处理
            if (!StringUtils.isEmpty(oldBoq.getParentCode())) {
                SysProfileBoq oldParent = mapper.getModelByCode(oldBoq.getParentCode());
                // 父项清单下是否有子节点
                int countOldChildren = mapper.countChildren(oldParent.getBoqCode());
                if (countOldChildren == 1) {
                    oldParent.setLeafNode(Constants.YES);
                    mapper.update(oldParent);
                }
            }

            if (!StringUtils.isEmpty(model.getParentCode())) {
                SysProfileBoq nowParent = mapper.getModelByCode(model.getParentCode());
                oldParentLevel = nowParent.getLevel();
                if (Constants.YES.equals(nowParent.getLeafNode())) {
                    nowParent.setLeafNode(Constants.NO);
                    mapper.update(nowParent);
                }
            }
        }

        // 计算级别差
        int level = oldParentLevel + 1;
        int dValue = level - oldLevel;
        if (oldParentCode.equals(nowParentCode)) {
            level = model.getLevel();
            dValue = 0;
        }
        // 计算内码
        if (StringUtils.isEmpty(model.getParentCode())) {
            nowAncestors = model.getBoqCode();
        } else {
            SysProfileBoq nowParent = mapper.getModelByCode(model.getParentCode());
            nowAncestors = nowParent.getAncestors() + "," + model.getBoqCode();
        }
        // 查询子清单
        List<SysProfileBoq> childrenList = mapper.selectChildrenList(oldBoq.getBoqCode());
        if (childrenList.size() > 0) {
            for (SysProfileBoq children : childrenList) {
                // 设置子节点的级别
                children.setLevel(children.getLevel() + dValue);
                children.setAncestors(children.getAncestors().replace(oldAncestors, nowAncestors));
                if (!model.getBoqCode().equals(oldBoq.getBoqCode())) {
                    if (children.getParentCode().equals(oldBoq.getBoqCode())) {
                        children.setParentCode(model.getBoqCode());
                    }
                }
            }
            mapper.updateBatch(childrenList);
        }
        // 设置清单
        model.setAncestors(nowAncestors);
        model.setLevel(level);
        model.setUpdateTime(DateUtils.getNowDate());
        mapper.update(model);
        return 1;
    }

    /**
     * 批量删除
     * 
     * @param ids 主键集合
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids) {
        return mapper.deleteByIds(ids);
    }

    /**
     * 删除
     * 
     * @param id 系统形象清单主键
     * @return 结果
     */
    @Override
    public int delete(Integer id) {
        return mapper.delete(id);
    }
}
